package com.lie.flat.controller;

import com.lie.flat.entity.CodeRequest;
import com.lie.flat.entity.CodeResponse;
import com.lie.flat.service.connect.CodeService;
import com.lie.flat.util.CommonReturn;
import com.lie.flat.util.IdempotentUtils;
import com.lie.flat.util.ValidatorUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/code")
public class CodeRequestControllder {
    @Autowired
    private CodeService codeService;

    /**
     * 调用第三方接口发送手机验证码
     *
     * @param codeRequest
     * @return
     */
    @PostMapping("/codeReq")
    @CrossOrigin
    public CommonReturn codeReq(@RequestBody CodeRequest codeRequest) {
        //接收手机验证码
        String str = RandomStringUtils.randomAlphanumeric(6);

        //king添加
        //验证手机号
        String mobilePhone = codeRequest.getMobilePhone();
        if (!IdempotentUtils.judge(IdempotentUtils.localCachePhone, mobilePhone)) {
            return CommonReturn.error("请30秒后再试!");
        }

        if (!ValidatorUtils.isLegalMobileNumber(codeRequest.getMobilePhone())) {
            return CommonReturn.error("手机号为空或者格式错误！");
        }

        codeRequest.setSysId("addd");
        codeRequest.setPasswd("2wsx#EDC");
        codeRequest.setSerialNo(str);

        //请求手机验证码
        CodeResponse codeResponse = codeService.jsonReqCode(codeRequest);

        if (!codeResponse.getCode().equals("C0001")) {
            return CommonReturn.error(codeResponse.getMessage());
        }
        return CommonReturn.success(codeResponse);
    }
}

